package com.tom.mapper.opencms;

import com.tom.entity.TomArticle;
import org.apache.ibatis.annotations.*;
import com.tom.vo.TomContentJson;

import java.util.Date;
import java.util.List;

@Mapper
public interface TomArticleMapper {

    @Select("<script>" +
            "SELECT AccessParameters " +
            "FROM tom_article " +
            "where publishdate between #{startDate} and #{endDate}" +
            "</script>")
    List<String> selectParamByPublish(String startDate, String endDate);
    @Select("<script>" +
            "SELECT AccessParameters " +
            "FROM tom_article " +
            "where publishdate between #{startDate} and #{endDate} and content_type is   null " +
            "</script>")
    List<String> selectParamByPublishNotContentType(String startDate, String endDate);

    @Select("<script>" +
            "SELECT AccessParameters " +
            "FROM tom_article " +
            "where publishdate between #{startDate} and #{endDate} and type= #{type}" +
            "</script>")
    List<String> selectParamByPublishAndType(String startDate, String endDate, Integer type);

    @Delete("<script>" +
            "DELETE FROM tom_article WHERE  articlepath=#{articlepath}"
            + "</script>")
    void deleteTomArticle(String articlepath);

    @Delete("<script>" +
            "DELETE FROM tom_content_json WHERE articlepath=#{articlepath}"
            + "</script>")
    void deleteTomContentJson(String articlePath);


    @Select("<script>" +
            "SELECT RESOURCE_ID FROM CMS_ONLINE_STRUCTURE WHERE RESOURCE_PATH=#{articlePath} " +
            "</script>")
    String selectResouceIdByResoucePath(String articlePath);

    @Delete("<script>" +
            "            DELETE FROM CMS_ONLINE_STRUCTURE WHERE RESOURCE_ID=#{resouceId}"
            + "</script>")
    void deleteCMS_ONLINE_STRUCTURE(String resouceId);

    @Delete("<script>" +
            "DELETE FROM CMS_OFFLINE_PROPERTIES WHERE PROPERTY_MAPPING_ID=#{resouceId}"
            + "</script>")
    void deleteCMS_OFFLINE_PROPERTIES(String resouceId);

    @Delete("<script>" +
            "DELETE FROM  CMS_OFFLINE_RESOURCES WHERE RESOURCE_ID=#{resouceId}"
            + "</script>")
    void deleteCMS_OFFLINE_RESOURCES(String resouceId);

    @Delete("<script>" +
            "DELETE FROM CMS_OFFLINE_CONTENTS  WHERE RESOURCE_ID=#{resouceId}"
            + "</script>")
    void deleteCMS_OFFLINE_CONTENTS(String resouceId);

    @Delete("<script>" +
            "DELETE FROM  CMS_OFFLINE_ACCESSCONTROL  WHERE RESOURCE_ID=#{resouceId}"
            + "</script>")
    void deleteCMS_OFFLINE_ACCESSCONTROL(String resouceId);

    @Delete("<script>" +
            "DELETE FROM CMS_OFFLINE_STRUCTURE WHERE RESOURCE_ID=#{resouceId}"
            + "</script>")
    void deleteCMS_OFFLINE_STRUCTURE(String resouceId);

    @Delete("<script>" +
            "DELETE FROM CMS_ONLINE_RESOURCES WHERE RESOURCE_ID=#{resouceId}"
            + "</script>")
    void deleteCMS_ONLINE_RESOURCES(String resouceId);

    @Select("<script>" +
            "SELECT j.articlepath,p.content_json_id FROM tom_article_price p" +
            " LEFT JOIN tom_content_json j " +
            " ON p.content_json_id=j.id where p.content_json_id>#{size} ORDER BY p.content_json_id ASC  LIMIT 1000"
            + "</script>")
    List<TomContentJson> selectAriclePath(Integer size);

    @Update(
            "<script>" +
                    "UPDATE tom_article_price SET article_path=#{articlePath} WHERE content_json_id=#{contentJsonId} "
                    + "</script>")
    void updateAriclePathByContentJsonId(String articlePath, Integer contentJsonId);

    @Select("<script>" +
            "  SELECT id  FROM tom_article WHERE articlepath=#{articlePath}  LIMIT 1"
            + "</script>")
    Integer selectAricleIdByAriclePath(String articlePath);

    @Update(
            "<script>" +
                    "UPDATE tom_article_price SET tom_article_id=#{articleId} WHERE content_json_id=#{content_json_id} "
                    + "</script>")
    void updateAricleIdByContentJsonId(Integer articleId, Integer content_json_id);

    @Select("select articlepath from tom_article where title=#{webTitle,jdbcType=VARCHAR} and accessParameters like concat('%',  #{accessParameters,jdbcType=VARCHAR},'%') order by createdate asc limit 1")
    String selectArticle(String webTitle, String accessParameters);


    /**
     * 根据文章类型和文章编号查找文章路径
     * @param accessParameter
     * @return
     */
    @Select("select articlepath from tom_article where AccessParameters like concat('%',#{accessParameter,jdbcType=VARCHAR},'%')")
    String selectAccessParameter(String accessParameter);

    @Insert("INSERT INTO `opencms`.`tom_failure_article` ( `AccessParameters`, `createdate`, `remark`)" +
            " VALUES (#{accessParameters},#{createdate} , #{remark})")
    void insertFailureArticle(String accessParameters, Date createdate, String remark);
    @Update(
            "<script>" +
                    "UPDATE `opencms`.`tom_article` SET  `content_type` = #{contentType} WHERE AccessParameters=#{p} "
                    + "</script>")
    void updateContentType(String p, int contentType);
    @Select("select AccessParameters FROM tom_article WHERE  publishdate BETWEEN #{startDate} and #{endDate} ")
    List<String> selectListAccessParameters(String startDate, String endDate);

    @Update("UPDATE `opencms`.`tom_article` SET  `paramsiteno` = #{siteNo}, `paramtype` = #{type}, `paramarticleno` = #{articleNo} WHERE `AccessParameters` = #{p};")
    void splitAccessParameters(String p, String siteNo, String type, String articleNo);
}
